#include <stdio.h>
#include "sds.h"
#include "adlist.h"
#include "../dict.h"
#include "redisassert.h"
#include <string.h>
#include "redis.h"

void sds_test() {
    sds empty = sdsempty();
    sdscat(empty,"hahah");
    size_t i = sdsavail(empty);
    printf("avail length:%d\n", i);
    
    sds s1 = sdsnew("XYXXYaXbcXYY");
    s1 = sdstrim(s1, "XY");
    printf("%s\n", s1);
}



/* ----------------------- StringCopy Hash Table Type ------------------------*/

static unsigned int _dictStringCopyHTHashFunction(const void *key)
{
    return dictGenHashFunction(key, strlen(key));
}

static void *_dictStringDup(void *privdata, const void *key)
{
    int len = strlen(key);
    char *copy = zmalloc(len+1);
    DICT_NOTUSED(privdata);

    memcpy(copy, key, len);
    copy[len] = '\0';
    return copy;
}

static int _dictStringCopyHTKeyCompare(void *privdata, const void *key1,
        const void *key2)
{
    DICT_NOTUSED(privdata);

    return strcmp(key1, key2) == 0;
}

static void _dictStringDestructor(void *privdata, void *key)
{
    DICT_NOTUSED(privdata);

    zfree(key);
}

dictType dictTypeHeapStringCopyKey = {
    _dictStringCopyHTHashFunction, /* hash function */
    _dictStringDup,                /* key dup */
    NULL,                          /* val dup */
    _dictStringCopyHTKeyCompare,   /* key compare */
    _dictStringDestructor,         /* key destructor */
    NULL                           /* val destructor */
};

/* This is like StringCopy but does not auto-duplicate the key.
 * It's used for intepreter's shared strings. */
dictType dictTypeHeapStrings = {
    _dictStringCopyHTHashFunction, /* hash function */
    NULL,                          /* key dup */
    NULL,                          /* val dup */
    _dictStringCopyHTKeyCompare,   /* key compare */
    _dictStringDestructor,         /* key destructor */
    NULL                           /* val destructor */
};

/* This is like StringCopy but also automatically handle dynamic
 * allocated C strings as values. */
dictType dictTypeHeapStringCopyKeyValue = {
    _dictStringCopyHTHashFunction, /* hash function */
    _dictStringDup,                /* key dup */
    _dictStringDup,                /* val dup */
    _dictStringCopyHTKeyCompare,   /* key compare */
    _dictStringDestructor,         /* key destructor */
    _dictStringDestructor,         /* val destructor */
};

void dict_test() {
    dict * dt = dictCreate(&dictTypeHeapStrings, NULL);
    dictEnableResize();
    int rt = dictAdd(dt, "even0", "24");
    rt = dictAdd(dt, "even1", "25");
    rt = dictAdd(dt, "even2", "26");
    rt = dictAdd(dt, "even3", "27");
    rt = dictAdd(dt, "even4", "28");
    rt = dictAdd(dt, "even5", "29");
}

void skiplist_test() {
    zskiplist* zl = zslCreate();
    robj* obj = createRawStringObject("even", 4);
    zskiplistNode *znode = zslInsert(zl, 0.2, obj);
}

int main(int argc, char** argv) {
    // sds_test();
    // dict_test();
    skiplist_test();
    printf("hello\n");
    return 0;
}